package com.fj.product.service.impl;

import com.fj.domain.Product;
import com.fj.product.dao.ProductDao;
import com.fj.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author fj
 * @date 2024/3/11 23:54
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public Product getProduct(Integer pid) {
        return productDao.getById(pid);
    }

    @Override
    public Product reduceStock(Integer pid, Integer number) {
        Product product = productDao.findById(pid).get();
        if (product.getStock()<number){
            throw new RuntimeException("库存不足");
        }
        product.setStock(product.getStock()-number);
        return productDao.save(product);
    }
}
